package com.dindang.ddm.mvp.ui.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.lang.ref.WeakReference;

/**
 * Created by cxw on & 2018/9/4
 */
public class AutoPollRecyclerView extends RecyclerView {
    private static final long TIME_AUTO_POLL = 16;
    AutoPollTask autoPollTask;
    private boolean running; //标示是否正在自动轮询
    private boolean canRun;//标示是否可以自动轮询,可在不需要的是否置false
    private int scrollX;
    private final int MSG_WHAT_SHUFFLING = 10086;
    /**
     * 剩余倒计时时间
     */
    private long mLastMillis = 5_000;
    /**
     * 间隔时间差(两次发送handler)
     */
    private long mIntervalMillis = 1_000;

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setNestedScrollingEnabled(false);
        autoPollTask = new AutoPollTask(this);
    }
    static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;

        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }
        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollX+=2;
                recyclerView.scrollBy(2, 2);
                if(recyclerView.getHeight() != 0 && recyclerView.scrollX % recyclerView.getHeight() == 0){
//                    recyclerView.stop();
                    recyclerView.mHandler.sendEmptyMessage(10086);
                }
                recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
            }
        }
    }
    //开启:如果正在运行,先停止->再开启
    public void start() {
        if (running)
            stop();
        canRun = true;
        running = true;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }
    public void stop() {
        running = false;
        removeCallbacks(autoPollTask);
    }
    public void onDestroy(){
        running = false;
        removeCallbacks(autoPollTask);
        mHandler.removeMessages(10086);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                if (running)
//                    stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
//                if (canRun)
//                    start();
                break;
        }
//        return super.onTouchEvent(e);//return  false，注释掉onTouchEvent()方法里面的stop和start方法，则列表自动滚动且不可触摸
        return false;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case MSG_WHAT_SHUFFLING:
                    if (mLastMillis > 0) {
                        stop();
                        mLastMillis -= mIntervalMillis;
                        mHandler.sendEmptyMessageDelayed(MSG_WHAT_SHUFFLING, mIntervalMillis);
                    } else {
                        mLastMillis = 5_000;
                        start();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeMessages(MSG_WHAT_SHUFFLING);
        stop();
    }
}
